//
//  RecentActivityController.m
//  kr_36
//
//  Created by lmt on 16/1/22.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecentActivityController.h"
#import "RecentActivityCell.h"
#import "RecentActivityModel.h"
#import "LMTAFNetwork.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>

#import "ActivityDetailController.h"
@interface RecentActivityController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *dataArr;
@property (nonatomic, assign)NSInteger i;/**< 记录加载 */

@end

@implementation RecentActivityController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"近期活动";
    //self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.i = 1;
    
    // 创建tableView
    [self creatTableView];
    
    // 数据解析
    [self dataHander];
    
    // 下拉刷新
    [self shuaxin];
    
    // 上拉加载
    [self jiazai];
    
}

#pragma mark - 上拉加载
- (void)jiazai
{
    MJRefreshAutoFooter *footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
       //延迟两秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            _i++;
            [LMTAFNetwork getUrl:[NSString stringWithFormat:@"https://rong.36kr.com/api/mobi/activity/list?page=%ld",self.i] body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
                
                NSLog(@"zxcc");
                
                NSArray *temp = resposeObject[@"data"][@"data"];
                NSMutableArray *aTemp = [RecentActivityModel modelHanderWithArray:temp];
                for (RecentActivityModel *m in aTemp) {
                    [self.dataArr addObject:m];
                }
                [_tableView reloadData];
                
            } failure:^(NSURLSessionDataTask *task, NSError *error) {
                
            }];
            [_tableView.mj_footer endRefreshing];
        });
    }];
    _tableView.mj_footer = footer;
    
}

#pragma mark - 下拉刷新
- (void)shuaxin
{
    //下拉刷新
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //延迟2秒
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)),
                       dispatch_get_main_queue(),^{
                           
                           // 刷新
                           [self dataHander];
                           //新版本是 MJ_header
                           [self.tableView.mj_header endRefreshing];
                       });
    }];
    //设置刷新控件
    self.tableView.mj_header = header;
    //设置文字
    header.arrowView.image = [UIImage imageNamed:@"jingxuan@3x"];
//    [header setTitle:@"使劲拉!" forState:MJRefreshStateIdle];
//    [header setTitle:@"松手刷!" forState:MJRefreshStatePulling];
//    [header setTitle:@"等！！！" forState:MJRefreshStateRefreshing];
    header.stateLabel.font = [UIFont systemFontOfSize:12];
    
}

#pragma mark - 数据解析
- (void)dataHander
{
    NSString *str = @"https://rong.36kr.com/api/mobi/activity/list?page=1";
    [LMTAFNetwork getUrl:str body:nil response:LMTJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
       
        NSArray *arr = resposeObject[@"data"][@"data"];
        self.dataArr = [RecentActivityModel modelHanderWithArray:arr];
        
        
        [_tableView reloadData];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:(UITableViewStylePlain)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerClass:[RecentActivityCell class] forCellReuseIdentifier:@"cell"];
    
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RecentActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    RecentActivityModel *m = _dataArr[indexPath.row];
    cell.title.text = m.activityName;
    cell.address.text = m.activityCity;
    cell.desc.text = m.activityDesc;
    cell.time.text = m.activityTime;
    cell.status.text = m.activityStatus;
    [cell.img sd_setImageWithURL:[NSURL URLWithString:m.activityImg]];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 285;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    ActivityDetailController *adVC = [[ActivityDetailController alloc] init];
    
    RecentActivityModel *m = _dataArr[indexPath.row];
    
    adVC.str = m.activityLink;

    [self.navigationController pushViewController:adVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
